package core

import (
	"animal/octopus/cache"
	"animal/octopus/common"
	"animal/octopus/orm"

	log "github.com/sirupsen/logrus"
	"gorm.io/gorm"
)

// ------------------------------------------------------------------------------
//
// ------------------------------------------------------------------------------
func GetTopoModuleList(topoViewID uint) ([]common.TopoModuleInfo, error) {
	return cache.Instance().GetTopoModuleList(topoViewID)
}

// ------------------------------------------------------------------------------
//
// ------------------------------------------------------------------------------
func UpdateTopoModule(db *gorm.DB, param common.UpdateTopoModuleParam) error {
	if err := common.DB(db).Transaction(func(tx *gorm.DB) error {
		if err := orm.UpdateTopoModule(tx, param); err != nil {
			log.WithField("param", common.MustMarshal(param)).
				Error("update topo module in database failed")
			return err
		}
		if err := cache.Instance().UpdateTopoModule(param); err != nil {
			log.WithField("param", common.MustMarshal(param)).
				Error("update topo module in cache failed")
			return err
		}
		return nil
	}); err != nil {
		log.Error("update topo module failed")
		return err
	}

	return nil
}
